import request from '@/utils/request'

// 获取用户列表
export const getUserList = (params) => {
  return request({
    url: '/users',
    method: 'get',
    params
  })
}

// 获取用户详情
export const getUserDetail = (id) => {
  return request({
    url: `/users/${id}`,
    method: 'get'
  })
}

// 创建用户
export const createUser = (data) => {
  return request({
    url: '/users',
    method: 'post',
    data
  })
}

// 更新用户
export const updateUser = (id, data) => {
  return request({
    url: `/users/${id}`,
    method: 'put',
    data
  })
}

// 删除用户
export const deleteUser = (id) => {
  return request({
    url: `/users/${id}`,
    method: 'delete'
  })
}

// 批量删除用户
export const batchDeleteUsers = (ids) => {
  return request({
    url: '/users/batch-delete',
    method: 'delete',
    data: { ids }
  })
}

// 启用/禁用用户
export const toggleUserStatus = (id, status) => {
  return request({
    url: `/users/${id}/status`,
    method: 'patch',
    data: { status }
  })
}

// 重置用户密码
export const resetUserPassword = (id) => {
  return request({
    url: `/users/${id}/reset-password`,
    method: 'post'
  })
}

// 分配角色
export const assignRoles = (id, roleIds) => {
  return request({
    url: `/users/${id}/roles`,
    method: 'put',
    data: { roleIds }
  })
}

// 获取用户角色
export const getUserRoles = (id) => {
  return request({
    url: `/users/${id}/roles`,
    method: 'get'
  })
}

// 获取用户权限
export const getUserPermissions = (id) => {
  return request({
    url: `/users/${id}/permissions`,
    method: 'get'
  })
}

// 导入用户
export const importUsers = (file) => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/users/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 导出用户
export const exportUsers = (params) => {
  return request({
    url: '/users/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}
